#include "BBContextManager.h"
#include "BBContext.h"


BBContextManager::BBContextManager( void )
{

}

BBContextManager::~BBContextManager( void )
{

}


void BBContextManager::removeContext( int tag )
{
	std::map<int,BBContext*>::iterator iter;
	if(m_pContextList.find(tag) != m_pContextList.end())
	{
		for(iter = m_pContextList.begin();iter!=m_pContextList.end();iter)
		{
			if((*iter).second->getTag() == tag)
			{
				m_pContextList.erase(iter);
				return;
			}
		}
	}
}

BBContext* BBContextManager::getContextByTag( int tag )
{
	if(m_pContextList.find(tag)!=m_pContextList.end())
	{
		std::map<int,BBContext*>::iterator iter;
		for(iter = m_pContextList.begin();iter!=m_pContextList.end();iter)
		{
			if((*iter).second->getTag() == tag)
			{
				return (*iter).second;
			}
		}
	}
	return NULL;
}


void BBContextManager::handlerContext( int tag )
{
	if(m_pContextList.find(tag)!=m_pContextList.end())
	{
		std::map<int,BBContext*>::iterator iter;
		for(iter = m_pContextList.begin();iter!=m_pContextList.end();iter)
		{
			if((*iter).second->getTag() == tag)
			{
				(*iter).second->handler();
			}
		}
	}
}

void BBContextManager::changeContextState( int tag,IState* state )
{
	if(m_pContextList.find(tag)!=m_pContextList.end())
	{
		if ( state == NULL) 
			return;
		BBContext* context = getContextByTag(tag);
		if(context != NULL)
		{
			context->changeContextState(state);
		}
	}
}

void BBContextManager::purgeContextManager()
{
	BB_SAFE_DELETE(_mInstance);
}
